﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using plas.core.entity;
using plas.common.api.domain;
using plas.common.api;
using DevComponents.DotNetBar.SuperGrid;
using System.Reflection;
using DevComponents.DotNetBar.Controls;


namespace plas.cps.ui
{
    public partial class DictionaryForm : AbstractForm
    {
        public DictionaryForm()
        {
            InitializeComponent();

        }


        Dictionary<string, string> dicOrders = new Dictionary<string, string>();

        AbstractForm tf;
        public AbstractForm Tf
        {
            get { return tf; }
            set { tf = value; }
        }
        private IDictionary dicService;

        public IDictionary DicService
        {
            get { return dicService; }
            set { dicService = value; }
        }
        private IDictionaryType typeService;

        public IDictionaryType TypeService
        {
            get { return typeService; }
            set { typeService = value; }
        }
        private DictionaryEntity editDictionary;

        public DictionaryEntity EditDictionary
        {
            get { return editDictionary; }
            set { editDictionary = value; }
        }

 
        private void DictionaryForm_Load(object sender, EventArgs e)
        {

            Dictionary<string, string> orders = new Dictionary<string, string>();            
            orders.Add("Code", OrderBy.ORDER_DESC);
            QueryResult<DictionaryTypeEntity> rel = this.typeService.QueryList(0, 0, null, orders);
            this.dictionaryTypeEntityBindingSource.DataSource = rel.ResultList;
            dicOrders.Add("Code", OrderBy.ORDER_DESC);
            MessageBox.Show("");
        }
        public int index = 0;
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butSave_Click(object sender, EventArgs e)
        {
            try
            {
                index = this.dataGridViewX1.CurrentRow.Index;
 
                DictionaryEntity dic = newDic();
                if (editDictionary == null)
                    dic.Operate = BaseEntity.OperateType.OPERATE_INSERT;
                else
                    dic.Operate = BaseEntity.OperateType.OPERATE_UPDATE;

                DictionaryTypeEntity type = (DictionaryTypeEntity)this.dictionaryTypeEntityBindingSource[index];

                this.dicService.Save(type, dic);
                editDictionary = null;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
 

        /// <summary>
        /// 动态获取DictionaryEntity 实体
        /// </summary>
        /// <returns></returns>
        private DictionaryEntity newDic()
        {

            DictionaryEntity dic = new DictionaryEntity();
            PropertyInfo[] tp = dic.GetType().GetProperties();
            Type formType = this.GetType();
            foreach (PropertyInfo pro in tp)
            {
                FieldInfo box = formType.GetField("dic" + pro.Name, BindingFlags.NonPublic | BindingFlags.Instance);
                if (box != null && box.FieldType == typeof(TextBoxX))
                {
                    pro.SetValue(dic, ((TextBoxX)box.GetValue(this)).Text, null);
                }

            }
            return dic;
        }
 

        private void dataGridViewX2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //if (newIndex < 0)
            //{
            //    newIndex = e.RowIndex;
            //    editDictionary = (DictionaryEntity)this.dictionaryEntityBindingSource[newIndex];
            //}
        }

        private void dataGridViewX1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //DictionaryTypeEntity type = (DictionaryTypeEntity)this.dictionaryTypeEntityBindingSource[e.RowIndex];
            //IList<DictionaryEntity> dics = type.Items;
            //type.Items = null;
            //type.Operate = BaseEntity.OperateType.OPERATE_UPDATE;

            //this.typeService.Save(type);
        }

        private void dataGridViewX1_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                index = this.dataGridViewX1.CurrentRow.Index;
                DictionaryTypeEntity type = (DictionaryTypeEntity)this.dictionaryTypeEntityBindingSource[index];
                Dictionary<string, string> orders = new Dictionary<string, string>();
                orders.Add("Code", OrderBy.ORDER_DESC);
                QueryResult<DictionaryEntity> rel = this.dicService.QueryList(0, 0, type, orders);
                dictionaryEntityBindingSource.DataSource = rel.ResultList;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
        }

        private void dataGridViewX2_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            //GridRow row = (GridRow)this.dataGridViewX2.ActiveGrid.Rows[e.RowIndex];
        }

        public void getDic(DictionaryEntity dic)
        {
            PropertyInfo[] tp = dic.GetType().GetProperties();
            Type formType = this.GetType();
            foreach (PropertyInfo pro in tp)
            {
                if ((pro.GetValue(dic, null)) == null)
                {
                    continue;
                }
                FieldInfo box = formType.GetField("dic" + pro.Name, BindingFlags.NonPublic | BindingFlags.Instance);
                if (box != null && box.FieldType == typeof(TextBoxX))
                {
                    ((TextBoxX)box.GetValue(this)).Text = pro.GetValue(dic, null).ToString();
                }


            }
       
        }

        private void dataGridViewX2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void butModify_Click(object sender, EventArgs e)
        {
            index = this.dataGridViewX1.CurrentRow.Index;

            DictionaryEntity dic = newDic();
            if (editDictionary == null)
                dic.Operate = BaseEntity.OperateType.OPERATE_INSERT;
            else
                dic.Operate = BaseEntity.OperateType.OPERATE_UPDATE;

            DictionaryTypeEntity type = (DictionaryTypeEntity)this.dictionaryTypeEntityBindingSource[index];

            this.dicService.Save(type, dic);
            editDictionary = null;
        }

   

    }
}
